package com.yasaka.stock.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yasaka.stock.model.LanguageList;
import com.yasaka.stock.model.TApiState;
import com.yasaka.stock.model.TLanguageType;
import com.yasaka.stock.model.TWebResult;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class TLanguage {

	public static final String APP_ID = "20160524000021877";
	public static final String SECURITY_KEY = "GsQ47W6V21Rd0qiuSNX_";

	ObjectMapper FJson = new ObjectMapper();
	
//	@Resource
//	private TUploadFile uploadFile;

	public TWebResult getStrlang(String aStr) {
		
		if("".equals(aStr))
			return new TWebResult(TApiState.API102,false);
		String mStr = aStr.substring(0,aStr.length() >200 ? 200 : aStr.length())
				.replaceAll("	"," ")
				.replaceAll("\\x0c", "")
				.replaceAll("\\x0b", "")
				.replaceAll("\\x1e", "")
				.replaceAll("\\x1f", "");

		TransApi api = new TransApi(APP_ID, SECURITY_KEY);
		String mResult = api.getTransResult(mStr, "auto", "en");

		Map<String, Object> mMap = null;

		try {
			mMap = FJson.readValue(mResult,
					new TypeReference<Map<String, Object>>() {
					});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new TWebResult(TApiState.API103, e.getMessage(), false);
		}		
		
		if (mMap != null) {
			String mError = (String) mMap.get("error_code");
			if ((mError != null) && (!"".equals(mError))){
				return new TWebResult(TApiState.API115,(String) mMap.get("error_msg"),false);				
			}
			TWebResult mRest = new TWebResult();
			mRest.setResult(true);
			Map<String, String> mMapRest = new HashMap<String, String>();
			List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
			mList.add(mMapRest);
			mRest.setData(mList);
			
			String baidulangId = (String) mMap.get("from");
			if ("".endsWith(baidulangId) || baidulangId.equals(null)) {
				mMapRest.put("langId","0");
				mMapRest.put("langName", "语种获取失败");

			} else {
				mMapRest.put("langId",
						LanguageList.getTransnLanguageId(TLanguageType.Baidu,baidulangId));
				mMapRest.put("langName", LanguageList.getLanguage(TLanguageType.Baidu,baidulangId));
			}
			return mRest;
		} else {
			return new TWebResult(TApiState.API115,false);		
		}	

	}	
	
	public TWebResult getLanguageForFileId(String aFileId) {
		
		if ((aFileId == null) || ("".endsWith(aFileId)))
			return new TWebResult(TApiState.API102, false);
		String mFileStr = null;
		try {
//			mFileStr = uploadFile.getFileTextForFileId(aFileId);
		} catch (Exception e) {
			return new TWebResult(TApiState.API109,e.getMessage(),false);
		}

		if (mFileStr == null)
			return new TWebResult(TApiState.API110, false);
		if ("nil".equals(mFileStr))
			return new TWebResult(TApiState.API106, false);
		return getStrlang(mFileStr);
		
	}
	
	public TWebResult googleTranslation(String aStr,String aFrom, String aTo){
		List <String> mList = new ArrayList<String>();
		try {
			mList.add(GoogleTranslate.translate(aStr, LanguageList.getGoogleLanguageForTransnId(aFrom),
					LanguageList.getGoogleLanguageForTransnId(aTo)));
		} catch (Exception e) {
			return new TWebResult(TApiState.API116,e.getMessage(),false);
		}
		return new TWebResult(mList); 
	}
	
	public TWebResult biyingTranslation(String aStr,String aFrom, String aTo){
		List <String> mList = new ArrayList<String>();
		try {
			mList.add(BiyingTranslate.translate(aStr, LanguageList.getBiyingForTransnId(aFrom),
					LanguageList.getBiyingForTransnId(aTo)));
		} catch (Exception e) {
			return new TWebResult(TApiState.API116,e.getMessage(),false);
		}
		return new TWebResult(mList); 
	}

}
